<?php
class ShippingModel extends Model {
	//根据订单获取运费
	public function getExpensesByOrder_id($order_id = 0){
		return 0;
	}
	public function getExpensesByProduct3($shipping_id = array(), $product = array(), $address = ''){
		$productsupplier = array();
	    foreach($product as $k => $v){
			$productsupplier[$v['supplier_id']][] = $v;
		}
		$ret = array();
		foreach($productsupplier as $key => $val){
			$ret[$key] = $this->getExpensesByProduct($shipping_id[$key], $val, $address);
		}
		return $ret;
	}
	
	
	//根据商品获取运费
	public function getExpensesByProduct($shipping_id = 0, $product = array(), $address = ''){
		$shipping = $this -> where(array('id' => $shipping_id)) -> find();
		//
		$shippingregiondb = M('Shippingregion');
		if($address != ''){
			$address = explode(',',trim($address, ','));
			foreach($address as $val){
				$shippingregion = $shippingregiondb -> where(array('shipping_id' => $shipping_id,'regionids' => array('like', '%,'.$val.',%'))) -> find();
				if($shippingregion){
					$shipping = $shippingregion;
				}
			}
		}

		$weight = 0;
		$total = 0;
		$extendweightexpenses = 0;
		$noshipping = true;
		$noshippingfee = true;
		foreach($product as $k => $v){
			if($v['need_shipping'] == 1){$noshipping = false;}
			$weight += $v['weight']*$v['num'];
			$total += $v['price']*$v['num'];
			if($v['noshippingfee'] == 0){$noshippingfee = false;}
	    }
		


		if($noshippingfee){
			return 0; 
		}
		if($noshipping){
			return 0;
		}
		//如果超过免费额度，设置免费
		if($total >= $shipping['free']){
			return 0;
		}
		$extend = $weight - $shipping['firstweight'];
		//echo $extend; 
		if($extend > 0){
			$extendweightexpenses = ceil($extend)*$shipping['extendweightexpenses'];
	    }
		
		$ret = $extendweightexpenses + $shipping['firstweightexpenses'];
		
		//$extend = $weight - $weight / ceil
		return $ret;
	}
    //根据商品获取运费
    public function getExpensesByProduct2($product = array(), $address = ''){
		$Message=array();
	    foreach($product as $k =>$v){
	        $shipping_id=$v['shipping_id'];
            if($v['need_shipping'] == 1){//不需物流
				$newweight=$Message[$shipping_id]['weight'];
				$newmoney=$Message[$shipping_id]['money'];
			}elseif($v['noshippingfee'] == 0){
				$newweight=$Message[$shipping_id]['weight'];
				$newmoney=$Message[$shipping_id]['money'];
			}else{
				$newweight=$Message[$shipping_id]['weight']+$v['weight']*$v['num'];
				$newmoney=$Message[$shipping_id]['money']+$v['price']*$v['num'];
			}
			//先统计金额，再统计重量
			$Message[$shipping_id]=array('weight'=>$newweight,'money'=>$newmoney);
        }
		//按供货商计算运费
		$ret=array();
		foreach($Message as $k => $v){
			if($k){
				$shipping = $this -> where(array('id' => $k)) -> find();
			}
			if($address != ''){
				$address = explode(',',trim($address, ','));
				foreach($address as $val){
					$shippingregion = M('Shippingregion') ->
					where(array('shipping_id' => $shipping_id,'regionids' => array('like', '%,'.$val.',%'))) -> find();
					if($shippingregion) $shipping = $shippingregion;
				}
			}
			$extendweightexpenses=0;
			if($v['money']>$shipping['free']){
				//满包邮
				$ret[$k]=0;
			}else{
				//不包邮（或者不满金额）就称重
				$weight=$v['weight'];
				$extend = $weight - $shipping['firstweight'];
				//echo $extend;
				if($extend > 0){
					$extendweightexpenses = ceil($extend)*$shipping['extendweightexpenses'];
				}
				$ret[$k]= $extendweightexpenses + $shipping['firstweightexpenses'];
			}
		}
		$allCash=array_sum($ret);
		return $allCash;
    }
	//输出一个快递单打印模板
	public function createPrintPage($order_id){
		$this->suppierids = M('Supplier_user') -> Field('supplier_id') -> where(array('user_id' => $_SESSION[C('USER_AUTH_KEY') ])) -> buildSql();
	    $this->is_suppierids = M('Supplier_user') -> Field('supplier_id') -> where(array('user_id' => $_SESSION[C('USER_AUTH_KEY') ])) -> count();
	    //$order_id = $this->_get('id');
		$saleDB = M("Sale"); 
		
		
		
		
		$map=array('s.order_id'=>$order_id);
		if($this->is_suppierids > 0){
			$map['s.supplier_id'] = array('in', $this->suppierids);
		}
		
		$sale = $saleDB->alias('s')->join("left join __PRODUCT__ as p on p.id=s.product_id")->where($map)->select();
		$attrvalDB = M('attr_val');
		foreach($sale as $k=>$v){
		   
		   $attrval = explode(';',$v['attr']);
		   $sale[$k]['attrmes'] = array();
		   
		   foreach($attrval as $v){ 
		     $mes = $attrvalDB->where(array('id'=>$v))->find();
			 array_push($sale[$k]['attrmes'],$mes['val']);
		   }
		   
		}
		
	}
}